package de.zefania.api;



import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import de.zefania.api.impl.JfaniaException;
import de.zefania.api.impl.ModulImpl;
import de.zefania.api.impl.PositionImpl;
import de.zefania.api.impl.Utilities;

/**
 * reads in the modul directory and creates Modul Objects 
 * @author Thomas
 *
 */
public class ModulManager {		
	private String modulPath;
	private String cachePath;
	private List<BibleModul> list = new ArrayList<BibleModul>();
	private HashMap<String, BibleModul> hashList = new HashMap<String, BibleModul>();
	private Logger logger = Logger.getLogger(ModulManager.class);
		
	/**
	 * creates an instance of the modul manager.<br />
	 * the Properties object must contain the following settings:
	 * <ul>
	 * <li>path.modul - the path where the modules are stored</li>
	 * <li>path.cache - the path where the modul cache is to be created</li>
	 * </ul> 
	 */
	public ModulManager(final Properties settings) {
		logger.setLevel(Level.ALL);
		this.modulPath = settings.getProperty("path.modul");
		this.cachePath = settings.getProperty("path.cache");
		readModulDirectory();		
	}
	
	private void readModulDirectory() {
		File modulDirectory = new File(modulPath);
		File[] modules = modulDirectory.listFiles(new Utilities.XmlFilter());
		for (File file : modules) {
			logger.trace("Reading modul '"+file.getName()+"'...");
			ModulImpl modul = new ModulImpl(file,this.cachePath);
			list.add(modul);
			hashList.put(modul.getDigest(), modul);
		}
	}
	/**
	 * returns the list of available moduls
	 * @return List of BibleModul objects
	 */
	public final List<BibleModul> getModulList() {
		return list;
		//return hashList.values();
	}
	
	/**
	 * parses a String to a Position object.
	 * the String is expected to be a human readable form
	 * e.g. "1. Mose 1,5-10"
	 * the position has to be a range of verses!
	 * NOT SUPPORTED: "1. Mose 1,5-10.12" 
	 * @param posString
	 * @return Position object 
	 * @throws JfaniaException
	 */
	public final Position parseHumanPosition(final String posString) throws JfaniaException {		
		return new PositionImpl(posString, PositionImpl.positionType.HUMAN);
	}
	/**
	 * parses an Mscope-formatted String to a Position object.
	 * (see http://wp1066500.wp101.webpack.hosteurope.de/zef/content/view/33/47/ 
	 * for more information about the Mscope-Format)
	 * @param posString
	 * @return Position object
	 * @throws JfaniaException
	 */
	public final Position parseMscopePosition(final String posString) throws JfaniaException {
		return new PositionImpl(posString, PositionImpl.positionType.MSCOPE);
	}
	
	public final BibleView getBibleView(final Position position, final BibleModul[] moduls) {
		return null;
	}
	/**
	 * returns a module by its hash value
	 * @param hash
	 * @return
	 */
	public final BibleModul getBibleModul(final String hash) {
		return hashList.get(hash);
	}
		
}
